package net.flyinggroup.paging.controller;

import net.flyinggroup.paging.model.Person;
import net.flyinggroup.paging.repository.PersonRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
public class PersonGraphQlController {
    private final PersonRepository repository;

    public PersonGraphQlController(PersonRepository repository) {
        this.repository = repository;
    }

    @QueryMapping
    public List<Person> allPeople() {
        return repository.findAll();
    }

    @QueryMapping
    public List<Person> allPeoplePaged(@Argument int page, @Argument int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return repository.findAll(pageRequest).toList();
    }
}
